<?php

/**
 * 默认控制器
 */
class Backend_Controller_Adminfonts extends Backend_Controller_Abstract
{
	function actionIndex()
	{
    	if(isset($_GET['ope'])){
    		if($_GET['ope'] == 'delete'){
    			$id = $this->_context->get('id', null);
    			$deleteFiles = $this->_context->get('deleteFiles', 0);
    			if($deleteFiles == 1){
    				$deleteFiles = true;
    			}else{
    				$deleteFiles = false;
    			}
    			if($id){
    				Fonts::model()->delete($deleteFiles, array('id' => $id));
    			}
    		}
    	}
    	
    	$this->_view['fonts'] = Fonts::model()->find('*', null, array('replaceDescriptionVars' => true, 'mergeType' => true));
    	
    }
	
	
	function actionAdd()
	{
		$formData = $this->_context->post('form', null);
		$fonts = new Fonts();
		if($formData){
			$formFiles = empty($_FILES['form'])? null : $_FILES['form'];
			$now = time();
			$fonts->attributes = array(
				'name' => empty($formData['name'])? '' : $formData['name'],
				'sketch' => empty($formData['sketch'])? '' : $formData['sketch'],
				'default_preview_string' => empty($formData['default_preview_string'])? '' : $formData['default_preview_string'],
				'root_foldername' => empty($formData['root_foldername'])? '' : $formData['root_foldername'],
				'type_using_id' => empty($formData['type_using_id'])? '' : $formData['type_using_id'],
				'type_area_id' => empty($formData['type_area_id'])? '' : $formData['type_area_id'],
				'type_feature_id' => empty($formData['type_feature_id'])? '' : $formData['type_feature_id'],
				'font_file' => empty($formFiles['name']['font_file'])? '' : Util::TimeFileName($formFiles['name']['font_file'], 'font_file', $now),
				'download_pack' => empty($formFiles['name']['download_pack'])? '' : Util::TimeFileName($formFiles['name']['download_pack'], 'download_pack', $now),
				'view_num' => 0,
				'created_date' => date('Y-m-d H:i:s', $now),
				'created_int' => $now
			);
			if($fonts->attributesIdentity(array('name', 'root_foldername'))){
				if($fonts->save()){
					$fonts->saveFiles($formFiles);
				}
			}
		}
		
		$this->_view['fonts'] = $fonts;
    	$this->_view['fonts_type'] = Types::model()->find('*', array('tablename' => 'fonts'), array('groupcolumnname' => true));
	}
	
	function actionEdit()
	{
		$id = $this->_context->get('id', null);
		$jqPlugins = new Jqueryplugins();
		$jqPlugins->attributes = $jqPlugins->find('*', array('id' => $id), array('onlyOne' => true, 'htmlSpecialcharsPreContent' => false));
		if(!empty($jqPlugins->attributes)){
			$formData = $this->_context->post('form', null);
			if($formData){
				$formFiles = empty($_FILES['form'])? null : $_FILES['form'];
				$now = time();
				$jqPlugins->attributes = array_merge(
					array('id' => $id),
						$formData
				);
				if(!empty($formFiles['name']['thumbnail'])){
					$jqPlugins->attributes['thumbnail'] = Util::TimeFileName($formFiles['name']['thumbnail'], 'thumb', $now);
				}
				if(!empty($formFiles['name']['download_pack'])){
					$jqPlugins->attributes['download_pack'] = Util::TimeFileName($formFiles['name']['download_pack'], 'download_pack', $now);
				}
				//if($jqPlugins->attributesIdentity(array('name', 'root_foldername'))){
					if($jqPlugins->save()){
						$jqPlugins->saveFiles($formFiles);
					}
				//}
				
			}
			
			$this->_view['jqPlugins'] = $jqPlugins;
    		$this->_view['jqueryplugins_type'] = Jquerypluginstype::model()->find();
		}
	}

	function actionGetPreviewImg()
	{
		$id = $this->_context->get('id', null);
		if($id){
			$font = new Fonts();
			
			$font->attributes = Fonts::model()->find('*', array('id' => $id), array('onlyOne' => true));
			
			$fontSize = $this->_context->get('font_size', null);
			$string = $this->_context->get('string', null);
			
			$font->printStringImg($fontSize, $string);
			
		}
		
		$this->_end();
	}	
	
}

